/**
 * 管理员数据操作的javabeen
 */
package com.aptech.restrant.Bean;

import java.sql.ResultSet;
import java.sql.SQLException;

import com.aptech.restrant.Model.AdminBean;

import db.DBConnect;

/**
 * 用户Bean类
 * @author Administrator
 *
 */
public class AdminDataBean {
	
	private ResultSet rs = null;
	
	/**
	 * 添加一个用户
	 * @param user 用户实体
	 * @return 影响的行数
	 */
	public int addAdmin(AdminBean Admin){
		int row = 0;
		try {
			String sql = "insert into tb_admin (name, password) values (?,?)";
			Object[] obj = new Object[]{Admin.getAdminName(),Admin.getAdminPass()};
			row = DBConnection.executeUpdate(sql,obj);		
		} catch (Exception e) {
			System.out.println("添加用户信息时出错！");
			e.printStackTrace();
		}
		return row;
	}
	
	/**
	 * 根据用户名查找一个用户Bean
	 * @param name 用户名
	 * @return 用户Bean
	 */
	public AdminBean findAdminByName(String name){
		AdminBean Admin = null;
		DBConnect dbc = new DBConnect();
		String sql = "select * from tb_admin where name = ?";
		dbc.prepareStatement(sql);
		try {
			dbc.setString(1, name);
			rs = dbc.executeQuery();
			if(rs.next()){
				Admin = new AdminBean();
				Admin.setAdminID(rs.getInt("adminID"));
				Admin.setAdminName(rs.getString("name"));
				Admin.setAdminPass(rs.getString("password"));
				
			}
		} catch (Exception e) {
			System.out.println("findAdminByName方法时出错");
			e.printStackTrace();
		} finally {
			dbc.close();
		}
		return Admin;
	}
	
	
	
	
	public boolean isAdmin(String username,String password){
		DBConnect dbc = new DBConnect();
		String sql = "select * from tb_admin where name=? and password=?";
		dbc.prepareStatement(sql);
		try {
			dbc.setString(1, username);
			dbc.setString(2, password);
			ResultSet rs = dbc.executeQuery();
			if(rs.next()){
				return true;
			}
			else{
				return false;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return false;
		}
		
	}
}
